package com.parknfly.mycalendar.calendar.adapter;


import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.parknfly.mycalendar.R;
import com.parknfly.mycalendar.calendar.model.SelectedRange;

import java.util.Calendar;


public class SubDateAdapter extends RecyclerView.Adapter<SubDateAdapter.VH> {


    private final int year, month;
    private final SelectedRange global;
    private final int firstDayOfWeek, daysInMonth, todayDay;
    private OnDateRangeListener listener;

    public SubDateAdapter(int year, int month,
                          SelectedRange global,
                          OnDateRangeListener l) {
        this.year = year;
        this.month = month;
        this.global = global;
        this.listener = l;

        Calendar tmp = Calendar.getInstance();
        tmp.set(year, month, 1);
        this.firstDayOfWeek = tmp.get(Calendar.DAY_OF_WEEK) - 1;
        this.daysInMonth = tmp.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 今天
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.YEAR) == year && now.get(Calendar.MONTH) == month) {
            todayDay = now.get(Calendar.DAY_OF_MONTH);
        } else {
            todayDay = 0;
        }
    }

    /* 工具：零时区 Calendar */
    private Calendar cal(int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    /* 区间工具：是否处于 [startCal , endCal] 内 */
    private boolean inRange(int day) {
        if (global.startCal == null) return false;
        Calendar d = cal(day);
        if (global.endCal == null) {
            return sameDay(d, global.startCal);
        }
        return !d.before(global.startCal) && !d.after(global.endCal);
    }

    private boolean sameDay(Calendar a, Calendar b) {
        return a.get(Calendar.YEAR) == b.get(Calendar.YEAR)
                && a.get(Calendar.DAY_OF_YEAR) == b.get(Calendar.DAY_OF_YEAR);
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup p, int viewType) {
        View v = LayoutInflater.from(p.getContext())
                .inflate(R.layout.sub_date_adapter, p, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH h, int pos) {
        if (pos < firstDayOfWeek || pos >= firstDayOfWeek + daysInMonth) {
            h.tvTitle.setText("");
            h.tvDay.setVisibility(INVISIBLE);
            h.tvMoney.setText("");
            h.leftView.setVisibility(GONE);
            h.rightView.setVisibility(GONE);
            h.tvTitle.setBackground(null);
            return;
        }
        int day = pos - firstDayOfWeek + 1;
        h.tvTitle.setText(String.valueOf(day));
        h.tvMoney.setText("¥" + day + "元/天");

        // 今天
        Calendar now = Calendar.getInstance();
        int todayDayOfMoney = now.get(Calendar.DAY_OF_MONTH);
        if (now.get(Calendar.YEAR) == year && now.get(Calendar.MONTH) == month && todayDayOfMoney == day) {
            h.tvDay.setText("今天");
            h.tvDay.setVisibility(VISIBLE);
        } else {
            h.tvDay.setVisibility(INVISIBLE); // 表示当前月份里没有“今天”
        }

        // 文字颜色
        if (todayDay > 0 && day < todayDay) {
            h.tvTitle.setTextColor(Color.parseColor("#BDBDBD"));
            h.viewCenter.setClickable(false);
            //之前的文字
            h.tvMoney.setText("");
        } else {
            //绘制已经出现的值
            h.tvTitle.setTextColor(Color.parseColor("#101010"));
            h.tvMoney.setTextColor(Color.parseColor("#101010"));
            h.viewCenter.setClickable(true);
            h.tvDay.setTextColor(Color.parseColor("#4180E9"));
        }

        // 重置背景
        h.viewCenter.setBackground(null);
        h.leftView.setVisibility(GONE);
        h.rightView.setVisibility(GONE);

        // 区间绘制
        boolean inRange = inRange(day);
        boolean isStart = global.startCal != null &&
                sameDay(cal(day), global.startCal);
        boolean isEnd = global.endCal != null &&
                sameDay(cal(day), global.endCal);

        if (inRange) {
            if (isStart) {
                h.viewCenter.setBackgroundResource(R.drawable.global_drawable_select);
                h.tvTitle.setTextColor(Color.parseColor("#ffffff"));
                h.tvMoney.setTextColor(Color.parseColor("#ffffff"));
                h.tvDay.setTextColor(Color.parseColor("#ffffff"));
                h.rightView.setVisibility(INVISIBLE);
            } else if (isEnd) {
                h.viewCenter.setBackgroundResource(R.drawable.global_drawable_select);
                h.tvTitle.setTextColor(Color.parseColor("#ffffff"));
                h.tvMoney.setTextColor(Color.parseColor("#ffffff"));
                h.tvDay.setTextColor(Color.parseColor("#ffffff"));
                h.leftView.setVisibility(INVISIBLE);
            } else {
                h.leftView.setVisibility(VISIBLE);
                h.rightView.setVisibility(VISIBLE);
            }
        } else if (isStart || isEnd) {
            h.viewCenter.setBackgroundResource(R.drawable.global_drawable_select);
        }


        // 点击事件
        h.viewCenter.setOnClickListener(v -> {
            if (todayDay > 0 && day < todayDay) return;
            Calendar clicked = cal(day);
            if (global.startCal == null ||
                    (global.startCal != null && global.endCal != null)) {
                // 第一次 或 重新选
                global.startCal = (Calendar) clicked.clone();
                global.endCal = null;
            } else {
                // 第二次
                if (clicked.before(global.startCal)) {
                    global.endCal = global.startCal;
                    global.startCal = (Calendar) clicked.clone();
                } else {
                    global.endCal = (Calendar) clicked.clone();
                }
            }
            if (listener != null) {
                listener.onRangeSelected(global.startCal, global.endCal);
            }
        });
    }

    @Override
    public int getItemCount() {
        return firstDayOfWeek + daysInMonth;// 空白 + 实际天数
    }


    static class VH extends RecyclerView.ViewHolder {

        TextView tvTitle, tvDay, tvMoney;

        View leftView, rightView;

        RelativeLayout viewCenter;

        public VH(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            rightView = itemView.findViewById(R.id.rightView);
            leftView = itemView.findViewById(R.id.leftView);
            viewCenter = itemView.findViewById(R.id.viewCenter);
            tvDay = itemView.findViewById(R.id.tvDay);
            tvMoney = itemView.findViewById(R.id.tvMoney);
        }
    }

    public interface OnDateRangeListener {
        void onRangeSelected(Calendar start, Calendar end);
    }


}
